package com.xframe.xdal.core.util.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.xframe.xdal.core.util.XReflect;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.util.Collection;

/**
 * PDF表格设定类
 * @author xufeng
 * @version 1.0
 * @date 2022-09-26
 *
 */
@Data
public class PdfTableOpt {
    /**
     * 表格总宽度
     */
    private float totalWidth;
    /**
     * 表格头部背景颜色
     */
    private BaseColor tableHeadBackgroundColor = XLineColor.LIGHT_GRAY;
    /**
     * 表格背景颜色
     */
    private BaseColor tableCellBackgroundColor = BaseColor.WHITE;
    /**
     * 表格前一个元素的间距
     */
    private int spacingBefore = 10;
    /**
     * 表格后一个元素的间距
     */
    private int spacingAfter = 10;
    /**
     * 表格头部字体大小
     */
    private int tableHeadFontSize = 10;
    /**
     * 表格头部字体风格
     */
    private int tableHeadFontMode = Font.NORMAL;
    /**
     * 表格头部字体颜色
     */
    private BaseColor tableHeadFontColor = BaseColor.BLACK;

    /**
     * 单元格字体大小
     */
    private int cellFontSize = 10;
    /**
     * 单元格字体风格
     */
    private int cellFontMode = Font.NORMAL;
    /**
     * 单元格字体颜色
     */
    private BaseColor cellFontColor = BaseColor.BLACK;

    /**
     * 交替行颜色
     */
    private BaseColor[] cellBaseColors = {BaseColor.WHITE,BaseColor.LIGHT_GRAY};
    /**
     * 数据单元格最小行高
     */
    private int cellMinimumHeight = 20;


    /**
     * 宿主对象
     */
    @Getter
    private PdfBuilder pdfBuilder;

    /**
     * 是否开启交替行颜色
     */
    private boolean supersedeFlag;

    @Getter
    private java.util.List<TableCol> tableColList;
    @Getter
    private Collection<Object> values;



    public PdfTableOpt(PdfBuilder pdfBuilder,
                       java.util.List<TableCol> tableColList,
                       Collection values,boolean supersedeFlag){
        this.pdfBuilder = pdfBuilder;
        this.totalWidth = this.pdfBuilder.getPageSize().getWidth();
        this.values = values;
        this.tableColList = tableColList;
        this.supersedeFlag = supersedeFlag;
    }


    /**
     * 构建表格
     */
    public void buildTabel() throws DocumentException, IOException{
        PdfPTable table = new PdfPTable(tableColList.size());
        table.setTotalWidth(this.totalWidth - 20);
        table.setLockedWidth(true);
        createTableHead(table);

        // 添加表格内容
        createTableContent(table);
        table.setSpacingBefore(this.spacingBefore);
        table.setSpacingAfter(this.spacingAfter);
        this.pdfBuilder.getDocument().add(table);
    }


    /**
     * 根据传入的标题名称：批量设置标题值
     *
     * @param table        创建的表格对象
     */
    private void createTableHead(PdfPTable table) throws DocumentException, IOException {

        //获取设置表格标题的字体
        Font font = PdfUtil.createFont(this.tableHeadFontSize, this.tableHeadFontMode, this.tableHeadFontColor);
        if(this.tableColList != null && this.tableColList.size() > 0){
            for (TableCol tableCol : tableColList) {
                PdfPCell cell = new PdfPCell(new Phrase(tableCol.getHeadName(), font));
                cell.setBackgroundColor(tableHeadBackgroundColor);
                cell.setMinimumHeight(20);
                cell.setUseAscender(true);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                table.addCell(cell);
            }
        }
    }

    /**
     * 创建表格内容
     *
     * @param table        表格对象
     */
    private void createTableContent(PdfPTable table) throws DocumentException, IOException {

        int index = 0;
        if(this.values != null && this.tableColList != null && this.tableColList.size() > 0){
            for (Object entry : this.values) {
                for(TableCol tableCol : tableColList){
                    String text = "";
                    Object value = XReflect.getFieldValueByAttrPath(entry,tableCol.getAttrName());
                    if(value != null){
                        text = value.toString();
                    }
                    PdfPCell cell = createPdfPCell(text, getCellBgColor(index));
                    table.addCell(cell);
                }
                index++;
            }
        }

    }



    /**
     * 获得数据单元格的背景颜色
     *
     * @param index 当前行号0开始
     */
    private BaseColor getCellBgColor(int index){
        if(!this.supersedeFlag){
            return this.tableCellBackgroundColor;
        }
        return cellBaseColors[index % 2];
    }

    /**
     * 设置表格内容
     *
     * @param text 标题内容
     * @param backgroundColor 背景颜色
     * @return 表格标题内容
     */
    private PdfPCell createPdfPCell(String text,BaseColor backgroundColor) throws DocumentException, IOException {
        Font font = PdfUtil.createFont(this.cellFontSize, this.cellFontMode, this.cellFontColor);
        PdfPCell cell = new PdfPCell(new Phrase(text, font));
        cell.setMinimumHeight(this.cellMinimumHeight);
        cell.setUseAscender(true);
        cell.setBackgroundColor(backgroundColor);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        return cell;
    }
}
